package cc.verywell.pureblock.net;

import java.io.Closeable;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.CompletionHandler;
import java.util.Objects;
import java.util.concurrent.Future;

/**
* 
* Copyright © 2020 TanYaqiu. All rights reserved.
* @author TanYaqiu 
* @address flat 601
* @date 2020年2月21日 下午10:23:31
*/
public class AioClient implements Closeable,AioClientInterface<NormalSession, AioClientConnector> {

	final private AioClientInterface<NormalSession,AioClientInner.ClientConfig> inner;
	final private AioClientOuter outer;
	public AioClient(AioProvider provider,AsynchronousChannelGroup channelThread) {
		Objects.requireNonNull(provider);
		this.inner = new AioClientInner(provider, channelThread);
		this.outer = new AioClientOuter(this.inner, provider);
	}
	
	@Override
	public <A> Future<NormalSession> connect(AioClientConnector remote, A attachment,CompletionHandler<NormalSession, A> handler) {
		return this.outer.connect(remote, attachment, handler);
	}
	
	public Future<NormalSession> connect(SocketAddress address){
		return this.inner.connect(()->address,null,null);
	}
	
	@Override
	public void close(){
		this.outer.close();
	}

	@Override
	public String toString() {
		return this.outer.toString();
	}
}
